package com.example.test.fragment;

import java.util.ArrayList;
import java.util.List;

import com.ericssonlabs.BarCodeTestActivity;
import com.example.test.BillListActivity;
import com.example.test.BindItemActivity;
import com.example.test.ChepaiLoction;
import com.example.test.R;
import com.example.test.demin.User;
import com.example.test.utils.DbDaoUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.zxing.activity.CaptureActivity;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class IndexFragmet extends BaseFragment {

	@ViewInject(R.id.tv_money)
	private TextView tv_money;

	private String username;
	private User user;
	@ViewInject(R.id.index_gridview)
	private GridView gridView;

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		sp = ct.getSharedPreferences("user", Context.MODE_PRIVATE);
		username = sp.getString("username", null);
		if (!(TextUtils.isEmpty(username))) {
			DbDaoUtils dao = new DbDaoUtils(ct);
			user = dao.findUserInfo(username);
			double money = user.getMoney();
			tv_money.setText(String.valueOf(money));
		}
	}

	@Override
	public void initData() {
		// TODO Auto-generated method stub
		sp = ct.getSharedPreferences("user", Context.MODE_PRIVATE);
		username = sp.getString("username", null);
		if (!(TextUtils.isEmpty(username))) {
			DbDaoUtils dao = new DbDaoUtils(ct);
			user = dao.findUserInfo(username);
			double money = user.getMoney();
			tv_money.setText(String.valueOf(money));
		}
		list.add(getString(R.string.codepay));
		list.add("车牌支付");
		list.add("银行卡");
		list.add("历史账单");
		list.add("充值缴费");
		list.add("支付详情");
		list2.add(getString(R.string.scan_two_dimensional_code_payment));
		list2.add("The payment by the license plate number");
		list2.add("Bank card");
		list2.add("History of bills");
		list2.add("recharge and pay");
		list2.add("Payment details");

		gridView.setAdapter(new GradviewAdpater());
		gridView.setOnItemClickListener(new OnItemClickListener() {

			private Intent intent;

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				switch (position) {
				case 0:
					Intent openCameraIntent = new Intent(ct,
							CaptureActivity.class);
					startActivity(openCameraIntent);
					break;
				case 1:
					intent = new Intent(ct, ChepaiLoction.class);
					startActivity(intent);
					break;
				case 2:
					intent = new Intent(ct, BindItemActivity.class);
					intent.putExtra("title", getString(R.string.bindcard));
					startActivity(intent);
					break;
				case 3:
					intent = new Intent(ct, BillListActivity.class);
					
					startActivity(intent);
					break;
				case 4:
					intent = new Intent(ct, BindItemActivity.class);
					intent.putExtra("title", getString(R.string.increase));
					startActivity(intent);
					break;
				case 5:
					 intent = new Intent(ct, ChepaiLoction.class);
					startActivity(intent);
					break;
				default:
					break;
				}
			}
		});
		// btn_saoma.setOnClickListener(this);
		// btn_chepai.setOnClickListener(this);
	}

	@Override
	public View initView(LayoutInflater inflater) {
		View view = inflater.inflate(R.layout.index_layout, null);
		ViewUtils.inject(this, view);
		return view;
	}

	private List<String> list = new ArrayList<String>();
	private int[] ids = { R.drawable.app_facepay, R.drawable.app_taxi,
			R.drawable.app_creditcard, R.drawable.app_note,
			R.drawable.app_transfer, R.drawable.app_freewifi };
	private List<String> list2=new ArrayList<String>();
	private int[] ids2 = { R.color.gridcolor1, R.color.gridcolor2,
			R.color.gridcolor3, R.color.gridcolor4, R.color.gridcolor5,
			R.color.gridcolor6 };

	class GradviewAdpater extends BaseAdapter {

		private TextView tv_griditem;
		private TextView tv_gridtext;
		private RelativeLayout rl_bg;

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			View view = View.inflate(ct, R.layout.grad_item_layout2, null);
			rl_bg = (RelativeLayout) view.findViewById(R.id.rl_bg_color);
			rl_bg.setBackgroundResource(ids2[position]);
			tv_griditem = (TextView) view.findViewById(R.id.tv_griditem);
			tv_griditem.setText(list.get(position));
			tv_gridtext = (TextView) view.findViewById(R.id.tv_gridtext);
			tv_gridtext.setText(list2.get(position));
			return view;
		}

	}
}
